$(function () {
    echart_bar();
    echart_map();
    loadPieData_a();
});

function loadPieData_a() {
    $.get('../static/data/fenxianfenbu.json', data => {
        // 此处要保证提供的json数据顺序和页面中图表顺序一致
        Object.keys(data.data).forEach((key, i) => {
            let seriesData = data.data[key].map(item => {
                item.name = item.type;
                return item;
            });
            echart_pie(`ec0${i}_pie`, seriesData)
        })
    })
}

//一带一路国家发展指数TOP10
function echart_bar() {
    let ec01_line = echarts.init($("#ec01_bar_trend")[0]);
    var dataArr = [820, 932, 901, 934, 1290, 1330, 1320, 1290, 1330, 1320];
    ec01_line.clear();
    ec01_line.setOption(com_bar_vertical);

    function setMarkPointer(data) {
        var res = [];
        $.each(data, function (i, item) {
            res.push({
                coord: [i, item],
                itemStyle: {
                    color: '#00337c'
                }
            })
        });
        return res
    }

    ec01_line.setOption({
        grid: {
            right: '2%',
        },
        xAxis: {
            type: 'category',
            data: ['国家一', '国家二', '国家三', '国家四', '国家五', '国家六', '国家七', '国家八', '国家九', '国家十'],
            axisLabel: {
                margin: 8 * bodyScale,
                fontSize: 12 * bodyScale
            },
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            showSymbol: false,
            smooth: true,
            data: dataArr,
            type: 'bar', itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(
                        0, 0, 0, 1,
                        [
                            {offset: 1, color: '#56ffd0'},
                            //{offset: 0.5, color: '#188df0'},
                            {offset: 0, color: '#32d8ff'}
                        ]
                    )
                }
            },
            markPoint: {
                symbolSize: 40,
                data: setMarkPointer(dataArr),
                label: {
                    show: true,
                    color: '#fff',
                    formatter: (data) => {
                        return dataArr[data.dataIndex]
                    }
                }
            }
        }]
    });
}

//风险分布
function echart_pie(toId, seriesData) {
    let ec01_pie = echarts.init($(`#${toId}`)[0]);
    ec01_pie.clear();
    ec01_pie.setOption(com_pie);
    ec01_pie.setOption({
        legend:{
            textStyle:{fontSize:8*bodyScale}
        },
        series: [
            {
                type: 'pie',
                radius: [20* bodyScale, 60* bodyScale],
                center: ['50%', '50%'],
                roseType: 'radius',
                label: {
                    normal: {
                        textStyle: {
                            color: '#666',
                            fontSize: 14 * bodyScale,
                        }
                    }
                },
                labelLine: {
                    normal: {
                        lineStyle: {
                            color: '#1f4c9d'
                        },
                        smooth: 0.2,
                        length: 5* bodyScale,
                        length2: 5* bodyScale
                    }
                },
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(
                            0, 0, 0, 1,
                            [
                                {offset: 1, color: '#1f4c9d'},
                                {offset: 0.5, color: '#45a9f5'},
                                {offset: 0, color: '#49e5cd'}
                            ]
                        ),
                        shadowBlur: 100,
                        shadowColor: 'rgba(0, 0, 0, 0.1)'
                    }
                },
                data: seriesData
            }
        ]
    });
}

//世界地图
const worldMapData = [
    {"name": "伊拉克", "value": 66.81, "latitude": 33.34058, "longitude": 44.400879},
    {"name": "哈萨克斯坦", "value": 66.12, "latitude": 43.25, "longitude": 76.949997},
    {"name": "伊朗", "value": 63, "latitude": 35.694389, "longitude": 51.421509},
    {"name": "沙特阿拉伯", "value": 62.36, "latitude": 24.687731, "longitude": 46.721851},
    {"name": "俄罗斯", "value": 61.92, "latitude": 55.75222, "longitude": 37.615555},
    {"name": "阿联酋", "value": 61.83, "latitude": 25.271139, "longitude": 55.307485},
    {"name": "科威特", "value": 61.61, "latitude": 29.21, "longitude": 47.56},
    {"name": "土库曼斯坦", "value": 61.35, "latitude": 37.950001, "longitude": 58.383331},
    {"name": "文莱", "value": 60, "latitude": 4.56, "longitude": 114.58},
    {"name": "印度尼西亚", "value": 59.59, "latitude": -6.21462, "longitude": 106.845131},
    {"name": "卡塔尔", "value": 58.69, "latitude": 25.286667, "longitude": 51.533333},
    {"name": "阿曼", "value": 58.31, "latitude": 23.36, "longitude": 58.37},
    {"name": "埃及", "value": 57.61, "latitude": 30.06263, "longitude": 31.24967},
    {"name": "印度", "value": 57.27, "latitude": 12.76111, "longitude": 80.010002},
    {"name": "越南", "value": 57.01, "latitude": 21.0245, "longitude": 105.841171},
    {"name": "阿塞拜疆", "value": 56.29, "latitude": 40.37767, "longitude": 49.89201},
    {"name": "罗马尼亚", "value": 55.78, "latitude": 44.432251, "longitude": 26.10626},
    {"name": "乌兹别克斯坦", "value": 55.52, "latitude": 41.264648, "longitude": 69.21627},
    {"name": "叙利亚", "value": 55.47, "latitude": 33.510201, "longitude": 36.291279},
    {"name": "马来西亚", "value": 55.35, "latitude": 3.1412, "longitude": 101.686531},
    {"name": "巴基斯坦", "value": 55.35, "latitude": 33.721481, "longitude": 73.043289},
    {"name": "菲律宾", "value": 55.28, "latitude": 14.6042, "longitude": 120.982201},
    {"name": "也门共和国", "value": 54.66, "latitude": 15.23, "longitude": 44.14},
    {"name": "乌克兰", "value": 54.64, "latitude": 50.433334, "longitude": 30.516666},
    {"name": "蒙古", "value": 54.21, "latitude": 47.55, "longitude": 106.53},
    {"name": "土耳其", "value": 54.1, "latitude": 36.924301, "longitude": 32.49131},
    {"name": "波兰", "value": 53.93, "latitude": 52.229771, "longitude": 21.01178},
    {"name": "缅甸", "value": 53, "latitude": 19.406, "longitude": 96.425},
    {"name": "波黑", "value": 52.46, "latitude": 43.52, "longitude": 18.26},
    {"name": "吉尔吉斯斯坦", "value": 52.45, "latitude": 42.869999, "longitude": 74.589996},
    {"name": "孟加拉国", "value": 51.93, "latitude": 23.588329, "longitude": 90.146942},
    {"name": "老挝", "value": 51.79, "latitude": 17.966669, "longitude": 102.599998},
    {"name": "白俄罗斯", "value": 51.69, "latitude": 53.026, "longitude": 27.552},
    {"name": "塞尔维亚", "value": 51.57, "latitude": 44.804008, "longitude": 20.46513},
    {"name": "格鲁吉亚", "value": 51.47, "latitude": 41.43, "longitude": 44.48},
    {"name": "阿富汗", "value": 51.47, "latitude": 34.52813, "longitude": 69.172333},
    {"name": "塔吉克斯坦", "value": 51.26, "latitude": 38.535751, "longitude": 68.779053},
    {"name": "约旦", "value": 50.66, "latitude": 31.955219, "longitude": 35.94503},
    {"name": "泰国", "value": 50.26, "latitude": 13.75, "longitude": 100.51667},
    {"name": "新加坡", "value": 49.84, "latitude": 1.28967, "longitude": 103.850067},
    {"name": "巴林", "value": 49.42, "latitude": 26.215361, "longitude": 50.583199},
    {"name": "尼泊尔", "value": 49.17, "latitude": 27.42, "longitude": 85.19},
    {"name": "斯里兰卡", "value": 48.42, "latitude": 6.93194, "longitude": 79.847778},
    {"name": "阿尔巴尼亚", "value": 48.34, "latitude": 41.2, "longitude": 19.48},
    {"name": "捷克共和国", "value": 47.94, "latitude": 48.2, "longitude": 17.2},
    {"name": "柬埔寨", "value": 46.74, "latitude": 11.6, "longitude": 104.8},
    {"name": "以色列", "value": 46.66, "latitude": 31.8, "longitude": 35.2},
    {"name": "东帝汶", "value": 46.6, "latitude": -8.35, "longitude": 125.35},
    {"name": "保加利亚", "value": 46.19, "latitude": 42.43, "longitude": 23.2},
    {"name": "斯洛伐克", "value": 45.99, "latitude": 48.148159, "longitude": 17.106741},
    {"name": "克罗地亚", "value": 45.71, "latitude": 45.8, "longitude": 16},
    {"name": "黑山", "value": 44.54, "latitude": 42.4426, "longitude": 19.2686},
    {"name": "爱莎尼亚", "value": 43.39, "latitude": 59.22, "longitude": 24.48},
    {"name": "斯洛文尼亚", "value": 42.44, "latitude": 45.578609, "longitude": 13.73611},
    {"name": "立陶宛", "value": 40.21, "latitude": 54.689159, "longitude": 25.2798},
    {"name": "匈牙利", "value": 39.37, "latitude": 47.4, "longitude": 19.2},
    {"name": "拉托维亚", "value": 39.31, "latitude": 56.58, "longitude": 24.8},
    {"name": "亚美尼亚", "value": 36.62, "latitude": 40.384281, "longitude": 43.873871},
    {"name": "马其顿", "value": 35.94, "latitude": 42, "longitude": 21.433331},
    {"name": "巴勒斯坦", "value": 35.64, "latitude": 31.42, "longitude": 35.12},
    {"name": "摩尔多瓦", "value": 33.92, "latitude": 47, "longitude": 28.5},
    {"name": "黎巴嫩", "value": 32.03, "latitude": 33.888939, "longitude": 35.494419},
    {"name": "不丹", "value": 28.63, "latitude": 27.466089, "longitude": 89.641907},
    {"name": "马尔代夫", "value": 23.76, "latitude": 4.1, "longitude": 73.28},
];
let [level1, level2, level3, level4, level5] = [[], [], [], [], []];
let seriesData = worldMapData.map(item => {
    return {
        name: item.name,
        value: [item.longitude, item.latitude, item.value,]
    }
});
seriesData.forEach(item => {
    let value = item.value[2]
    switch (true) {
        case value < 40:
            level1.push(item);
            break;
        case value < 50:
            level2.push(item);
            break;
        case value < 60:
            level3.push(item);
            break;
        case value < 70:
            level4.push(item);
            break;
        case value < 100:
            level5.push(item);
            break;
    }

});

function echart_map() {
    let ec01_map = echarts.init($("#ec01_map")[0]);

    let option = {
        legend: {
            right: '10%',
            bottom:'25%',
            orient: 'vertical',
            textStyle:{
                color:"#fff",
                fontSize:12*bodyScale
            }
        },
        geo: {
            map: 'world',
            itemStyle: {
                normal: {
                    areaColor: '#11172f',
                    borderColor: '#2377fe15'
                },
                emphasis: {
                    areaColor: '#2377fe15'
                }
            }
        },
        series: [
            {
                name: '高风险',
                itemStyle: {color: '#e5225a'},
                data: level1,
            }, {
                name: '较高风险',
                itemStyle: {color: '#1a1094'},
                data: level2,
            }, {
                name: '中等风险',
                itemStyle: {color: '#00d2ff'},
                data: level3,
            }, {
                name: '较低风险',
                itemStyle: {color: '#2377fe'},
                data: level4,
            }, {
                name: '低风险',
                itemStyle: {color: '#77b2ff'},
                data: level5,
            }
        ].map(item => {
            return $.extend({}, {
                type: 'effectScatter',
                coordinateSystem: 'geo',
                itemStyle: {
                    emphasis: {
                        // borderWidth: 1
                    }
                },
                rippleEffect: {
                    period: 3,
                    scale: 6 * bodyScale,
                    brushType: 'stroke'
                },
                symbolSize: 4 * bodyScale,
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: false
                    }
                },
            }, item)
        })
    };
    ec01_map.setOption(option);
}
